package com.example.trackcircuitdiagnosis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.trackcircuitdiagnosis.entity.DataEntity;
import com.example.trackcircuitdiagnosis.exception.HttpResult;
import com.example.trackcircuitdiagnosis.exception.HttpResultGenerator;
import com.example.trackcircuitdiagnosis.service.DataService;
import com.example.trackcircuitdiagnosis.vo.DataVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/data")
public class DataController {


    @Autowired
    private DataService dataService;

    // 新增或者更新
    @PostMapping
    public boolean save(@RequestBody DataEntity data) {
        return dataService.saveOrUpdate(data);
    }

    @DeleteMapping("/{id}")

    public Boolean delete(@PathVariable Integer id) {
        return dataService.removeById(id);
    }

    @PostMapping("/del/batch")
    @Operation(
            description = "批量删除",
            parameters = {
                    @Parameter(name = "ids", description = "要批量删除的信息")
            }

    )
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return dataService.removeByIds(ids);
    }

    @GetMapping
    @Operation(description = "查找全部")
    public List<DataEntity> findAll() {
        return dataService.list();
    }

    @GetMapping("/{id}")
    @Operation(
            description = "查找模拟量信息",
            parameters = {
                    @Parameter(name = "id", description = "要查找信息id")
            }

    )
    public DataEntity findOne(@PathVariable Integer id) {
        return dataService.getById(id);
    }

    @GetMapping("/page")

    @Operation(
            description = "分页查询",
            parameters = {
                    @Parameter(name = "pageNum", description = "要查找信息页数"),
                    @Parameter(name = "pageSize", description = "要查找信息每一页的数量")
            }

    )
    public Page<DataEntity> findPage(@RequestParam Integer pageNum,
                                     @RequestParam Integer pageSize) {
        QueryWrapper<DataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return dataService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @GetMapping("/find-all-data")
    @Operation(description = "查找全部区域对应模拟量信息")
    public HttpResult find() {
        List<DataVo> dataVos = dataService.getDataWithAnalog();
        return HttpResultGenerator.success(dataVos);


    }




}

